﻿namespace GSC.WebControls
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Collections.Specialized;
    using System.Reflection;
    using System.Web.UI;
    using System.IO;
	internal static class AjaxFileUploadUtility
	{
		private static MethodInfo encodeStringMethodInfo;

		static AjaxFileUploadUtility()
		{
			Assembly assembly = typeof(ScriptManager).Assembly;
			Type pageRequestManagerType = assembly.GetType("System.Web.UI.PageRequestManager");
			encodeStringMethodInfo = pageRequestManagerType.GetMethod(
				"EncodeString", BindingFlags.Static | BindingFlags.NonPublic);
		}

		internal static void WriteScriptBlock(HttpResponse response, bool begin)
		{
			string scriptBegin = "<script type='text/javascript' language='javascript'>window.__f__=function(){/*";
			string scriptEnd = "*/}</script>";

			response.Write(begin ? scriptBegin : scriptEnd);
		}

		internal static void EncodeString(TextWriter writer, string type, string id, string content)
		{
			encodeStringMethodInfo.Invoke(null, new object[] { writer, type, id, content });
		}

		internal static bool IsInIFrameAsyncPostBack(NameValueCollection requestBody)
		{ 
			string[] values = requestBody.GetValues("__AjaxFileUploading__");

			if (values == null) return false;

			foreach (string value in values)
			{
				if (value == "__IsInAjaxFileUploading__")
				{
					return true;
				}
			}

			return false;
		}
	}
}
